/*
 * main.cpp
 *
 *  Created on: Jan 19, 2010
 *      Author: nrqm
 *
 * This example program will configure the radio as a receiver and wait for a packet.
 *  When the packet is received the radio will assert the IRQ pin.  The driver will
 *  handle the interrupt and call the radio_rxhandler function defined below in this
 *  file.  If you don't call the Radio_Receive function at some point, then the radio
 *  will stop receiving after it queues up three packets.
 *
 * This example has the radio's Vcc pin connected to the Seeeduino's pin 48.  This
 *  allows the radio's power to be cycled programatically.  Cycling the radio is not
 *  strictly necessary, but it can be useful if you run into driver bugs.  The complete
 *  pin mapping is as follows:
 *
 * Radio		Seeeduino
 *  Vcc				48
 *   CE				8
 *  CSN				7
 *  SCK				52
 * MOSI				51
 * MISO				50
 *  IRQ				2
 *  GND				GND
 *
 * If you don't like the CE and CSN pins way off on the other side of the board then
 *  you can change them to another digital I/O port (they're defined at the top of
 *  radio.cpp).  The IRQ pin can be moved to another interrupt pin (see online docs
 *  for attachInterrupt function; also the interrupt number is hardcoded in the driver
 *  because I'm irresponsible).
 */

#include "../arduinomega/WProgram.h"
#include "../arduinomega/radio/radio.h"

uint8_t rx_addr[RADIO_ADDRESS_LENGTH] = { 0x05, 0x04, 0x03, 0x02, 0x01 };//hovercraft address
uint8_t tx_addr[RADIO_ADDRESS_LENGTH] = { 0x01, 0x02, 0x03, 0x04, 0x05 };//remote joystick address

// An extensible packet structure for transmitting data over the radio.  See packet.h for
// more information.
radiopacket_t packet;

extern "C" void __cxa_pure_virtual(){};

int main()
{
	// Initialize the Arduino stuff
	init();

	// Test the led
	pinMode(13, OUTPUT);
	digitalWrite( 13, HIGH );
	delay( 500 );
	digitalWrite( 13, LOW );

	pinMode(48, OUTPUT);	//Radio
	// Cycle the radio's power by clearing the power pin for a second and re-setting it.
	digitalWrite(48, LOW);
	delay(1000);
	digitalWrite(48, HIGH);
	// The radio needs 10.5 ms upon power-up.
	delay(11);

	Serial.begin( BAUD_RATE );
	Serial.println( "Radio Powered Up" );
	Serial.println( "Initializing Radio...");
	// Initialize the SPI connection, configure the I/O pins, and set the register defaults
	Radio_Init();
	Serial.println( "Radio Initialized" );
	// Configure pipe 0 on the radio
	Radio_Configure_Rx(RADIO_PIPE_0, rx_addr, ENABLE);
	// Configure the radio's data rate (must match the other radio) and the broadcast power
	Radio_Configure(RADIO_2MBPS, RADIO_HIGHEST_POWER);
	Serial.println("Radio Receiver Configured");

	// main() should never return.  This will do nothing until the radio interrupt is fired.
	for (;;);

	return 0;
}

/* This function is called by the radio driver whenever the radio receives a packet.
 */
void radio_rxhandler(uint8_t pipe_number)
{
	//Serial.begin( BAUD_RATE );
	//Serial.println( "packet received: MESSAGE_ID - 41" );

	// Copy the received packet from the radio to the local data structure
	Radio_Receive(&packet);
	if (packet.type == MESSAGE && packet.payload.message.messageid == 41)
	{
		digitalWrite(13, HIGH);
		Serial.println( "packet received: MESSAGE_ID - 41" );
	}
}
